using System.ComponentModel;
using System.Runtime.Serialization;

using SharpGL.Enumerations;

namespace SharpGL.OpenGLAttributes
{
    /// <summary>
    /// The line attributes.
    /// </summary>
    [TypeConverter(typeof(System.ComponentModel.ExpandableObjectConverter))]
    [DataContract(/*IsReference = true, */Name = "LineAttributes", Namespace = "SharpGL.SceneGraph")]
    public class LineAttributes : OpenGLAttributes
    {
        #region Private Consts

        private static readonly AttributeMask attributeMask = AttributeMask.Line;

        #endregion Private Consts

        #region Private Fields

        [DataMember()]
        private float? width;
        [DataMember()]
        private bool? smooth;

        #endregion Private Fields

        #region Protected Methods

        protected override void OnDeserializing()
        {
            AttributeFlags = attributeMask;
        }

        #endregion Protected Methods

        #region Public Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="LineAttributes"/> class.
        /// </summary>
        public LineAttributes()
        {
            AttributeFlags = attributeMask;
        }

        #endregion Public Constructors

        #region Public Methods

        /// <summary>
        /// Returns true if any attributes are set.
        /// </summary>
        /// <returns>
        /// True if any attributes are set
        /// </returns>
        public override bool AreAnyAttributesSet()
        {
            return width.HasValue ||
                smooth.HasValue;
        }

        /// <summary>
        /// Reset all attributes to unset state.
        /// </summary>
        public override void ResetAllAttributes()
        {
            width = null;
            smooth = null;
        }

        /// <summary>
        /// Sets the attributes.
        /// </summary>
        /// <param name="gl">The OpenGL instance.</param>
        public override void SetAttributes(OpenGL gl)
        {
            if (width.HasValue) gl.LineWidth(width.Value);
            if (smooth.HasValue) gl.EnableIf(OpenGL.GL_LINE_SMOOTH, smooth.Value);
        }

        #endregion Public Methods

        #region Public Properties

        /// <summary>
        /// Gets or sets the width.
        /// </summary>
        /// <value>
        /// The width.
        /// </value>
        [Description("Width"), Category("Line attributes")]
        public float? Width
        {
            get { return width; }
            set
            {
                if (width != value)
                {
                    width = value;

                    NotifyPropertyChanged("Width");
                }
            }
        }

        /// <summary>
        /// Gets or sets the smooth.
        /// </summary>
        /// <value>
        /// The smooth.
        /// </value>
        [Description("Smooth"), Category("Line attributes")]
        public bool? Smooth
        {
            get { return smooth; }
            set
            {
                if (smooth != value)
                {
                    smooth = value;

                    NotifyPropertyChanged("Smooth");
                }
            }
        }

        #endregion Public Properties
    }
}
